package com.easygoods.service;

import com.bestvike.stone.core.lang.BeanUtils;
import com.bestvike.stone.core.lang.Convert;
import com.bestvike.stone.spring.exception.BusinessException;
import com.bestvike.stone.spring.exception.ErrorCode;
import com.bestvike.stone.spring.page.PageableResponse;
import com.bestvike.stone.spring.service.BaseService;
import com.easygoods.dao.DrawCashInfoDao;
import com.easygoods.entity.DrawCashInfo;
import com.easygoods.enums.YNEnum;
import com.easygoods.request.DrawCashListRequest;
import com.easygoods.request.DrawCashRequest;
import com.easygoods.response.DrawCashListResponse;
import com.easygoods.shiro.auth.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by 许崇雷 on 2019-02-20.
 */
@Service
public class DrawCashService extends BaseService {
    @Autowired
    private CashService cashService;
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private DrawCashInfoDao drawCashInfoDao;

    //提现
    @SuppressWarnings("unused")
    public void drawCash(DrawCashRequest request) {
        final DrawCashInfo drawCashInfo = this.cashService.drawCash(request.getPayPwd(), request.getAmount(), "用户提现");
        final UserAuth userAuth = this.getAuth();
        if (userAuth.getChannel() == null)
            throw new BusinessException(ErrorCode.ERROR_SERVER, "暂不支持的渠道");
        switch (userAuth.getChannel()) {
            case WeChat:
                //TODO 微信接口需要有一个月现金流水才能调用
                //this.weChatService.drawCash(drawCashInfo);//转账
                break;
            case AliPay:
                throw new BusinessException(ErrorCode.ERROR_SERVER, "暂不支持支付宝");
            default:
                throw new BusinessException(ErrorCode.ERROR_SERVER, "敬请期待");
        }
    }

    //查询提现列表
    public PageableResponse<DrawCashListResponse> getDrawCashList(DrawCashListRequest request) {
        final UserAuth userAuth = this.getAuth();
        final Example example = new Example(DrawCashInfo.class);
        example.createCriteria().andEqualTo(DrawCashInfo.MEMBER_ID, userAuth.getMemberId());
        example.orderBy(DrawCashInfo.CREATE_TIME).asc();
        final List<DrawCashInfo> drawCashInfos = this.drawCashInfoDao.selectByExampleAndRowBounds(example, request.toRowBounds());
        List<DrawCashListResponse> responseList = BeanUtils.toList(drawCashInfos, DrawCashListResponse.class);
        return PageableResponse.create(drawCashInfos, responseList);
    }

    //重新发起提现
    public void redrawCash(String drawCashId) {
        final UserAuth userAuth = this.getAuth();
        final Example example = new Example(DrawCashInfo.class);
        example.createCriteria().andEqualTo(DrawCashInfo.ID, drawCashId)
                .andEqualTo(DrawCashInfo.MEMBER_ID, userAuth.getMemberId());
        final DrawCashInfo drawCashInfo = this.drawCashInfoDao.selectOneByExample(example);
        if (Convert.toType(drawCashInfo.getTransferFinished(), YNEnum.class) == YNEnum.Y)
            throw new BusinessException(ErrorCode.ERROR_SERVER, "已提现不能重复操作");
        if (userAuth.getChannel() == null)
            throw new BusinessException(ErrorCode.ERROR_SERVER, "暂不支持的渠道");
        switch (userAuth.getChannel()) {
            case WeChat:
                //TODO 微信接口需要有一个月现金流水才能调用
                //this.weChatService.drawCash(drawCashInfo);//转账
                break;
            case AliPay:
                throw new BusinessException(ErrorCode.ERROR_SERVER, "暂不支持支付宝");
            default:
                throw new BusinessException(ErrorCode.ERROR_SERVER, "敬请期待");
        }
    }
}
